package com.tjshop.android;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.tjshop.android.adapter.pinglun_adapter;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.Comment;
import com.tjshop.android.util.HttpUtil;
import com.tjshop.android.util.MD5Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 活动详情界面。
 * 
 * @author FIMH
 * @since 2015-01-03
 */
public class PinglunActivity extends BaseActivity{

	private Context mContext;
	private ArrayList<Comment> pinglun_lists = new ArrayList<Comment>();
	private pinglun_adapter mSimpleAdapter;
	private EditText pinglun_content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pinglun);
		setTheme(R.style.Theme_Sherlock);
		updateCommonReturnBar("评论列表");
		mContext = this;

		ListView listView = (ListView) findViewById(R.id.pinglun_listview);
		mSimpleAdapter = new pinglun_adapter(getApplicationContext(),
				pinglun_lists, getLayoutInflater());
		listView.setAdapter(mSimpleAdapter);
		pinglun_content = (EditText) findViewById(R.id.inputContent);
		String coupon_id = "";
		try {
			Bundle bundle = getIntent().getExtras();
			coupon_id = bundle.getString("id");
			GetPinglunList(coupon_id, 0 + "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		final String final_coupon_id = coupon_id;
		findViewById(R.id.SendPinglun).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (GlobeVars.isLogin) {
							if (!pinglun_content.getText().toString()
									.equals("")) {
								SendPinglun(final_coupon_id,
										GlobeVars.UserInfos.getId(),
										pinglun_content.getText().toString());
							} else {
								Toast.makeText(mContext, "评论不能为空",
										Toast.LENGTH_SHORT).show();
							}
						} else {
							Toast.makeText(mContext, "请先登录", Toast.LENGTH_SHORT)
									.show();
						}
					}
				});
	}

	void SendPinglun(String coupon_id, String user_id, String content) {
		String md5 = MD5Utils.md5(coupon_id + user_id + content
				+ AppConfig.SignKey);
		final String param = "coupon_id=" + coupon_id + "&user_id=" + user_id
				+ "&content=" + content + "&sign=" + md5;
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message message = new Message();
				String ret = HttpUtil.sendPost(AppConfig.SendPinglunUrl, param);
				if (!ret.equals("")) {
					try {
						JSONObject json = new JSONObject(ret);
						if (json.getInt("code") == 0) {
							message.what = 2;
							mHandler.sendMessage(message);
						} else {
							message.what = 3;
							mHandler.sendMessage(message);
						}
					} catch (JSONException e) {
						message.what = 3;
						mHandler.sendMessage(message);
						e.printStackTrace();
					}
				}

			}
		}).start();
	}

	void GetPinglunList(String coupon_id, String offset) {
		String md5 = MD5Utils.md5(coupon_id + offset + AppConfig.SignKey);
		final String param = "coupon_id=" + coupon_id + "&offset=" + offset
				+ "&sign=" + md5;
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message message = new Message();
				String ret = HttpUtil.sendPost(AppConfig.GetPinglunListUrl,
						param);
				if (!ret.equals("")) {
					try {
						JSONObject json = new JSONObject(ret);
						if (json.getInt("code") == 0) {
							JSONArray jsonArr = json.getJSONObject("data")
									.getJSONArray("list");
							for (int i = 0; i < jsonArr.length(); i++) {
								JSONObject item = jsonArr.getJSONObject(i);
//								Comment info = new Comment();
//								info.setId(item.getString("id"));
//								info.setUser_id(item.getString("user_id"));
//								info.setContent(item.getString("content"));
//								info.setAdd_time(item.getString("add_time"));
//								info.setUser_nick(item.getString("user_nick"));
//								info.setHeader(item.getString("header"));
//								pinglun_lists.add(info);
							}
							message.what = 0;
							mHandler.sendMessage(message);
						} else {
							message.what = 1;
							mHandler.sendMessage(message);
						}
					} catch (JSONException e) {
						message.what = 1;
						mHandler.sendMessage(message);
						e.printStackTrace();
					}
				}

			}
		}).start();
	}

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 0:
			Toast.makeText(mContext, "获取评论成功!", Toast.LENGTH_SHORT).show();
			mSimpleAdapter.notifyDataSetChanged();
			break;
		case 1:
			Toast.makeText(mContext, "获取评论失败", Toast.LENGTH_SHORT).show();
			break;
		case 2:
			Toast.makeText(mContext, "评论成功!", Toast.LENGTH_SHORT).show();
			Comment info = new Comment();
//			info.setHeader(GlobeVars.UserInfos.getHeading_image());
//			info.setContent(pinglun_content.getText().toString());
//			info.setUser_nick(GlobeVars.UserInfos.getName());
			pinglun_lists.add(info);
			mSimpleAdapter.notifyDataSetChanged();
			break;
		case 3:
			Toast.makeText(mContext, "评论失败", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		return false;
	}
}
